using System;
using System.Collections;
using System.Collections.Generic;

namespace LightCAD.Three
{
    public class Spherical
    {
        #region Properties

        public double radius;
        public double phi;
        public double theta;

        #endregion

        #region constructor
        public Spherical(double radius = 1, double phi = 0, double theta = 0)
        {
            this.radius = radius;
            this.phi = phi; // polar angle
            this.theta = theta; // azimuthal angle
        }
        #endregion

        #region methods
        public Spherical set(double radius, double phi, double theta)
        {
            this.radius = radius;
            this.phi = phi;
            this.theta = theta;
            return this;
        }
        public Spherical copy(Spherical other)
        {
            this.radius = other.radius;
            this.phi = other.phi;
            this.theta = other.theta;
            return this;
        }
        public Spherical makeSafe()
        {
            double EPS = 0.000001;
            this.phi = JMath.max(EPS, JMath.min(JMath.PI - EPS, this.phi));
            return this;
        }
        public Spherical setFromVector3(Vector3 v)
        {
            return this.setFromCartesianCoords(v.x, v.y, v.z);
        }
        public Spherical setFromCartesianCoords(double x, double y, double z)
        {
            this.radius = JMath.sqrt(x * x + y * y + z * z);
            if (this.radius == 0)
            {
                this.theta = 0;
                this.phi = 0;
            }
            else
            {
                this.theta = JMath.atan2(x, z);
                this.phi = JMath.acos(MathUtils.clamp(y / this.radius, -1, 1));
            }
            return this;
        }
        public Spherical clone()
        {
            return new Spherical().copy(this);
        }
        #endregion

    }
}
